package com.focusai.efairy.business.update;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.focusai.efairy.R;
import com.focusai.efairy.model.manager.CacheManager;


/**
 * Created by zgj on 2016/5/30.
 */
public class UpdateDialog extends Dialog implements View.OnClickListener {

    private Context context;

    private MyDialogListener listener;

    private Button confirmBtn, cancleBtn;

    private TextView tvlatestVersion, tvlatestVersionSize, tvUpdateInfo;

    private CheckBox checkBox;

    private boolean showCheckBox = true;

    private String versionInfoString = "", updateInfoString = "";

    int versionCode;

    public interface MyDialogListener {
        void onClick(View view, boolean check);
    }

    public UpdateDialog(Context context) {
        super(context);
    }


    public UpdateDialog(Context context, int themeResId,int versionCode, MyDialogListener listener, boolean showCheckBox) {
        super(context, themeResId);
        this.listener = listener;
        this.showCheckBox = showCheckBox;
        this.context = context;
        this.versionCode = versionCode;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_update_app);
        confirmBtn = (Button) findViewById(R.id.confirm_btn);
        cancleBtn = (Button) findViewById(R.id.cancle_btn);
        confirmBtn.setOnClickListener(this);
        cancleBtn.setOnClickListener(this);
        tvlatestVersion = (TextView) findViewById(R.id.latest_version);
        tvlatestVersion.setText(versionInfoString);
        tvlatestVersionSize = (TextView) findViewById(R.id.latest_version_size);
        tvUpdateInfo = (TextView) findViewById(R.id.update_info);
        tvUpdateInfo.setText(updateInfoString);
        tvUpdateInfo.setMovementMethod(new ScrollingMovementMethod());
        checkBox = (CheckBox) findViewById(R.id.check_btn);
        checkBox.setOnClickListener(this);
        if (!showCheckBox) {
            findViewById(R.id.checkLayout).setVisibility(View.GONE);
            cancleBtn.setVisibility(View.GONE);
        }
    }

    public void initUpdataInfo(String content,String versionName){
        tvUpdateInfo.setText(content);
        tvlatestVersion.setText(versionName);
    }

    public void setUpdateVersion(String version) {
        this.versionInfoString = "最新版本：" + version;
    }

    public void setTvUpdateInfo(String info) {
        this.updateInfoString = info;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == R.id.check_btn) {
            if (checkBox.isChecked()) {
                CacheManager.setIsgoreVersion(versionCode);
            } else {
                CacheManager.setIsgoreVersion(0);
            }
        } else {
            this.cancel();
        }
        listener.onClick(v, checkBox.isChecked());
    }

    public void setUpdateInfoMaxHeight(){
        WindowManager windowManager = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
        Display d = windowManager.getDefaultDisplay(); // 获取屏幕宽、高用
        int height = (int) (d.getHeight() * 0.4);
        tvUpdateInfo.setMaxHeight(height);
    }

    public void showDialog(boolean isForce) {
        this.setCanceledOnTouchOutside(false);
        this.setCancelable(!isForce);
        this.show();
    }
}
